{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d22dc24f",
   "metadata": {},
   "source": [
    "# 1. Python使用API模版"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "b992a321",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "af9c7411",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. url\n",
    "base_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "\n",
    "# 2. api账户、通行证\n",
    "API_key = 'iE1MiracWI1O1jmSAZWUrhdS7FOvV8My'\n",
    "API_secret = '8B16HiL0padJMA1cBvkYCU0lPJ8hqZ_l'\n",
    "\n",
    "# 3. 请求方法：POST\n",
    "\n",
    "# 4. 准备payload（酬载）\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_secret,\n",
    "    'image_url':'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2F1114%2F0G320105A7%2F200G3105A7-4-1200.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1650456613&t=0a0339de28564e9e67260a644dac3d1c'\n",
    "}\n",
    "\n",
    "# 5. 发起请求\n",
    "r = requests.post(base_url, params = payload)\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "4f9fc6e9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "4f4532bc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b'{\"request_id\":\"1648277911,be474fe6-c356-44b1-bdb6-1bffca2125cf\",\"time_used\":219,\"faces\":[{\"face_token\":\"7e1037a9775a913a293eeb6040b6633c\",\"face_rectangle\":{\"top\":162,\"left\":263,\"width\":287,\"height\":287}}],\"image_id\":\"qFfLt/x2abgIGaBHbcf1pg==\",\"face_num\":1}\\n'"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "bb9c2580",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648277911,be474fe6-c356-44b1-bdb6-1bffca2125cf',\n",
       " 'time_used': 219,\n",
       " 'faces': [{'face_token': '7e1037a9775a913a293eeb6040b6633c',\n",
       "   'face_rectangle': {'top': 162, 'left': 263, 'width': 287, 'height': 287}}],\n",
       " 'image_id': 'qFfLt/x2abgIGaBHbcf1pg==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "0d13d8e6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'7e1037a9775a913a293eeb6040b6633c'"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_token = r.json()[\"faces\"][0][\"face_token\"]\n",
    "face_token"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "55e2e045",
   "metadata": {},
   "source": [
    "# 2. 人脸分析"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "c38b3120",
   "metadata": {},
   "outputs": [],
   "source": [
    "analyze_url = 'https://api-cn.faceplusplus.com/facepp/v3/face/analyze'\n",
    "# 1. api账户、通行证\n",
    "API_key = 'iE1MiracWI1O1jmSAZWUrhdS7FOvV8My'\n",
    "API_secret = '8B16HiL0padJMA1cBvkYCU0lPJ8hqZ_l'\n",
    "\n",
    "# 2. 准备payload（酬载）\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_secret,\n",
    "    'face_tokens':face_token,\n",
    "    'return_attributes':'gender,age,smiling,headpose,facequality,blur,eyestatus,emotion,beauty,mouthstatus,eyegaze,skinstatus'\n",
    "}\n",
    "\n",
    "# 3. 发起请求\n",
    "r_analyze = requests.post(analyze_url, params = payload)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "5f8eb4b1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_analyze"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "d7ad6e96",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 232,\n",
       " 'request_id': '1648277968,695c6867-5c47-45a4-b093-f4246e8eed0b',\n",
       " 'faces': [{'attributes': {'emotion': {'sadness': 0.23,\n",
       "     'neutral': 44.318,\n",
       "     'disgust': 1.328,\n",
       "     'anger': 0.23,\n",
       "     'surprise': 0.249,\n",
       "     'fear': 49.06,\n",
       "     'happiness': 4.585},\n",
       "    'beauty': {'female_score': 81.515, 'male_score': 80.094},\n",
       "    'gender': {'value': 'Female'},\n",
       "    'age': {'value': 23},\n",
       "    'mouthstatus': {'close': 99.977,\n",
       "     'surgical_mask_or_respirator': 0.0,\n",
       "     'open': 0.023,\n",
       "     'other_occlusion': 0.0},\n",
       "    'glass': {'value': 'None'},\n",
       "    'skinstatus': {'dark_circle': 2.795,\n",
       "     'stain': 8.692,\n",
       "     'acne': 0.363,\n",
       "     'health': 94.061},\n",
       "    'headpose': {'yaw_angle': -10.614029,\n",
       "     'pitch_angle': 8.482851,\n",
       "     'roll_angle': 0.39508888},\n",
       "    'blur': {'blurness': {'threshold': 50.0, 'value': 0.361},\n",
       "     'motionblur': {'threshold': 50.0, 'value': 0.361},\n",
       "     'gaussianblur': {'threshold': 50.0, 'value': 0.361}},\n",
       "    'smile': {'threshold': 50.0, 'value': 71.848},\n",
       "    'eyestatus': {'left_eye_status': {'normal_glass_eye_open': 0.008,\n",
       "      'no_glass_eye_close': 0.0,\n",
       "      'occlusion': 0.0,\n",
       "      'no_glass_eye_open': 99.992,\n",
       "      'normal_glass_eye_close': 0.0,\n",
       "      'dark_glasses': 0.0},\n",
       "     'right_eye_status': {'normal_glass_eye_open': 0.0,\n",
       "      'no_glass_eye_close': 0.0,\n",
       "      'occlusion': 0.0,\n",
       "      'no_glass_eye_open': 100.0,\n",
       "      'normal_glass_eye_close': 0.0,\n",
       "      'dark_glasses': 0.0}},\n",
       "    'facequality': {'threshold': 70.1, 'value': 74.788},\n",
       "    'eyegaze': {'right_eye_gaze': {'position_x_coordinate': 0.46,\n",
       "      'vector_z_component': 0.995,\n",
       "      'vector_x_component': -0.096,\n",
       "      'vector_y_component': -0.02,\n",
       "      'position_y_coordinate': 0.423},\n",
       "     'left_eye_gaze': {'position_x_coordinate': 0.468,\n",
       "      'vector_z_component': 0.995,\n",
       "      'vector_x_component': 0.071,\n",
       "      'vector_y_component': -0.07,\n",
       "      'position_y_coordinate': 0.39}}},\n",
       "   'face_rectangle': {'width': 287, 'top': 162, 'left': 263, 'height': 287},\n",
       "   'face_token': '7e1037a9775a913a293eeb6040b6633c'}]}"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_analyze.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "a17d5c3d",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2ccd385b",
   "metadata": {},
   "source": [
    "## 数据处理小魔法 pandas.json_normalize() 实现数据表格化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "925ae35d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>face_token</th>\n",
       "      <td>7e1037a9775a913a293eeb6040b6633c</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.sadness</th>\n",
       "      <td>0.23</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.neutral</th>\n",
       "      <td>44.318</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.disgust</th>\n",
       "      <td>1.328</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.anger</th>\n",
       "      <td>0.23</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.surprise</th>\n",
       "      <td>0.249</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.fear</th>\n",
       "      <td>49.06</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.happiness</th>\n",
       "      <td>4.585</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.beauty.female_score</th>\n",
       "      <td>81.515</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.beauty.male_score</th>\n",
       "      <td>80.094</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.gender.value</th>\n",
       "      <td>Female</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.age.value</th>\n",
       "      <td>23</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.mouthstatus.close</th>\n",
       "      <td>99.977</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.mouthstatus.surgical_mask_or_respirator</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.mouthstatus.open</th>\n",
       "      <td>0.023</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.mouthstatus.other_occlusion</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.glass.value</th>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.skinstatus.dark_circle</th>\n",
       "      <td>2.795</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.skinstatus.stain</th>\n",
       "      <td>8.692</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.skinstatus.acne</th>\n",
       "      <td>0.363</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.skinstatus.health</th>\n",
       "      <td>94.061</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.headpose.yaw_angle</th>\n",
       "      <td>-10.614029</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.headpose.pitch_angle</th>\n",
       "      <td>8.482851</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.headpose.roll_angle</th>\n",
       "      <td>0.395089</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.blur.blurness.threshold</th>\n",
       "      <td>50.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.blur.blurness.value</th>\n",
       "      <td>0.361</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.blur.motionblur.threshold</th>\n",
       "      <td>50.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.blur.motionblur.value</th>\n",
       "      <td>0.361</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.blur.gaussianblur.threshold</th>\n",
       "      <td>50.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.blur.gaussianblur.value</th>\n",
       "      <td>0.361</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.smile.threshold</th>\n",
       "      <td>50.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.smile.value</th>\n",
       "      <td>71.848</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.left_eye_status.normal_glass_eye_open</th>\n",
       "      <td>0.008</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.left_eye_status.no_glass_eye_close</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.left_eye_status.occlusion</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.left_eye_status.no_glass_eye_open</th>\n",
       "      <td>99.992</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.left_eye_status.normal_glass_eye_close</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.left_eye_status.dark_glasses</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.right_eye_status.normal_glass_eye_open</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.right_eye_status.no_glass_eye_close</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.right_eye_status.occlusion</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.right_eye_status.no_glass_eye_open</th>\n",
       "      <td>100.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.right_eye_status.normal_glass_eye_close</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.right_eye_status.dark_glasses</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.facequality.threshold</th>\n",
       "      <td>70.1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.facequality.value</th>\n",
       "      <td>74.788</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.right_eye_gaze.position_x_coordinate</th>\n",
       "      <td>0.46</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.right_eye_gaze.vector_z_component</th>\n",
       "      <td>0.995</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.right_eye_gaze.vector_x_component</th>\n",
       "      <td>-0.096</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.right_eye_gaze.vector_y_component</th>\n",
       "      <td>-0.02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.right_eye_gaze.position_y_coordinate</th>\n",
       "      <td>0.423</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.left_eye_gaze.position_x_coordinate</th>\n",
       "      <td>0.468</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.left_eye_gaze.vector_z_component</th>\n",
       "      <td>0.995</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.left_eye_gaze.vector_x_component</th>\n",
       "      <td>0.071</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.left_eye_gaze.vector_y_component</th>\n",
       "      <td>-0.07</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.left_eye_gaze.position_y_coordinate</th>\n",
       "      <td>0.39</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_rectangle.width</th>\n",
       "      <td>287</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_rectangle.top</th>\n",
       "      <td>162</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_rectangle.left</th>\n",
       "      <td>263</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_rectangle.height</th>\n",
       "      <td>287</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                                                   0\n",
       "face_token                                          7e1037a9775a913a293eeb6040b6633c\n",
       "attributes.emotion.sadness                                                      0.23\n",
       "attributes.emotion.neutral                                                    44.318\n",
       "attributes.emotion.disgust                                                     1.328\n",
       "attributes.emotion.anger                                                        0.23\n",
       "attributes.emotion.surprise                                                    0.249\n",
       "attributes.emotion.fear                                                        49.06\n",
       "attributes.emotion.happiness                                                   4.585\n",
       "attributes.beauty.female_score                                                81.515\n",
       "attributes.beauty.male_score                                                  80.094\n",
       "attributes.gender.value                                                       Female\n",
       "attributes.age.value                                                              23\n",
       "attributes.mouthstatus.close                                                  99.977\n",
       "attributes.mouthstatus.surgical_mask_or_respirator                               0.0\n",
       "attributes.mouthstatus.open                                                    0.023\n",
       "attributes.mouthstatus.other_occlusion                                           0.0\n",
       "attributes.glass.value                                                          None\n",
       "attributes.skinstatus.dark_circle                                              2.795\n",
       "attributes.skinstatus.stain                                                    8.692\n",
       "attributes.skinstatus.acne                                                     0.363\n",
       "attributes.skinstatus.health                                                  94.061\n",
       "attributes.headpose.yaw_angle                                             -10.614029\n",
       "attributes.headpose.pitch_angle                                             8.482851\n",
       "attributes.headpose.roll_angle                                              0.395089\n",
       "attributes.blur.blurness.threshold                                              50.0\n",
       "attributes.blur.blurness.value                                                 0.361\n",
       "attributes.blur.motionblur.threshold                                            50.0\n",
       "attributes.blur.motionblur.value                                               0.361\n",
       "attributes.blur.gaussianblur.threshold                                          50.0\n",
       "attributes.blur.gaussianblur.value                                             0.361\n",
       "attributes.smile.threshold                                                      50.0\n",
       "attributes.smile.value                                                        71.848\n",
       "attributes.eyestatus.left_eye_status.normal_gla...                             0.008\n",
       "attributes.eyestatus.left_eye_status.no_glass_e...                               0.0\n",
       "attributes.eyestatus.left_eye_status.occlusion                                   0.0\n",
       "attributes.eyestatus.left_eye_status.no_glass_e...                            99.992\n",
       "attributes.eyestatus.left_eye_status.normal_gla...                               0.0\n",
       "attributes.eyestatus.left_eye_status.dark_glasses                                0.0\n",
       "attributes.eyestatus.right_eye_status.normal_gl...                               0.0\n",
       "attributes.eyestatus.right_eye_status.no_glass_...                               0.0\n",
       "attributes.eyestatus.right_eye_status.occlusion                                  0.0\n",
       "attributes.eyestatus.right_eye_status.no_glass_...                             100.0\n",
       "attributes.eyestatus.right_eye_status.normal_gl...                               0.0\n",
       "attributes.eyestatus.right_eye_status.dark_glasses                               0.0\n",
       "attributes.facequality.threshold                                                70.1\n",
       "attributes.facequality.value                                                  74.788\n",
       "attributes.eyegaze.right_eye_gaze.position_x_co...                              0.46\n",
       "attributes.eyegaze.right_eye_gaze.vector_z_comp...                             0.995\n",
       "attributes.eyegaze.right_eye_gaze.vector_x_comp...                            -0.096\n",
       "attributes.eyegaze.right_eye_gaze.vector_y_comp...                             -0.02\n",
       "attributes.eyegaze.right_eye_gaze.position_y_co...                             0.423\n",
       "attributes.eyegaze.left_eye_gaze.position_x_coo...                             0.468\n",
       "attributes.eyegaze.left_eye_gaze.vector_z_compo...                             0.995\n",
       "attributes.eyegaze.left_eye_gaze.vector_x_compo...                             0.071\n",
       "attributes.eyegaze.left_eye_gaze.vector_y_compo...                             -0.07\n",
       "attributes.eyegaze.left_eye_gaze.position_y_coo...                              0.39\n",
       "face_rectangle.width                                                             287\n",
       "face_rectangle.top                                                               162\n",
       "face_rectangle.left                                                              263\n",
       "face_rectangle.height                                                            287"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.json_normalize(r_analyze.json()['faces']).T"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9ee62520",
   "metadata": {},
   "source": [
    "# 3. 人脸对比"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1b1cc9a5",
   "metadata": {},
   "source": [
    "## 3.1 人脸检测获取toekn"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "fe0e94f3",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. url\n",
    "base_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "\n",
    "# 2. api账户、通行证\n",
    "API_key = 'iE1MiracWI1O1jmSAZWUrhdS7FOvV8My'\n",
    "API_secret = '8B16HiL0padJMA1cBvkYCU0lPJ8hqZ_l'\n",
    "\n",
    "# 3. 请求方法：POST\n",
    "\n",
    "# 4. 准备payload（酬载）\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_secret,\n",
    "    'image_url':'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Finews.gtimg.com%2Fnewsapp_bt%2F0%2F10985943880%2F641.jpg&refer=http%3A%2F%2Finews.gtimg.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1650870232&t=86ab568cb4ad2ab2a9917af637c67650'\n",
    "}\n",
    "\n",
    "# 5. 发起请求\n",
    "r = requests.post(base_url, params = payload)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "a2564d05",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'40a008704bc3ece393de7f6769832d46'"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_token01 = r.json()[\"faces\"][0]['face_token']\n",
    "face_token01"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "65f48aba",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. url\n",
    "base_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "\n",
    "# 2. api账户、通行证\n",
    "API_key = 'iE1MiracWI1O1jmSAZWUrhdS7FOvV8My'\n",
    "API_secret = '8B16HiL0padJMA1cBvkYCU0lPJ8hqZ_l'\n",
    "\n",
    "# 3. 请求方法：POST\n",
    "\n",
    "# 4. 准备payload（酬载）\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_secret,\n",
    "    'image_url':'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fqqpublic.qpic.cn%2Fqq_public%2F0%2F0-2587108314-6594DB0995FBBA388B26C2E35C730CDE%2F0%3Ffmt%3Djpg%26amp%3Bsize%3D45%26amp%3Bh%3D797%26amp%3Bw%3D512%26amp%3Bppv%3D1&refer=http%3A%2F%2Fqqpublic.qpic.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1650870232&t=fbf52493f18616b79f2052950e693b68'\n",
    "}\n",
    "\n",
    "# 5. 发起请求\n",
    "r = requests.post(base_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "e31d89d8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'89e21ad845442cfc6421d009151cfabd'"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_token02 = r.json()[\"faces\"][0]['face_token']\n",
    "face_token02"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "05ddb755",
   "metadata": {},
   "source": [
    "## 3.2 人脸对比"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "2af9de85",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. url\n",
    "compare_url = 'https://api-cn.faceplusplus.com/facepp/v3/compare'\n",
    "\n",
    "\n",
    "# 2. api账户/通行证\n",
    "API_key = 'iE1MiracWI1O1jmSAZWUrhdS7FOvV8My'\n",
    "API_secret = '8B16HiL0padJMA1cBvkYCU0lPJ8hqZ_l'\n",
    "\n",
    "# 3. 请求方法：POST\n",
    "\n",
    "# 4. 准备payload（酬载）\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_secret,\n",
    "    'face_token1':face_token01,\n",
    "    'face_token2':face_token02\n",
    "}\n",
    "\n",
    "# 5. 发起请求\n",
    "r = requests.post(url = compare_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "d36a82f6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "88899dfd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648278720,0c07853d-5301-46a9-ba85-4a17f53ab05d',\n",
       " 'time_used': 361,\n",
       " 'confidence': 84.894,\n",
       " 'thresholds': {'1e-3': 62.327, '1e-4': 69.101, '1e-5': 73.975}}"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8bbd4ebb",
   "metadata": {},
   "source": [
    "# 4. 封装人脸检测函数，实现快速调用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "d400aaaa",
   "metadata": {},
   "outputs": [],
   "source": [
    "API_key = 'iE1MiracWI1O1jmSAZWUrhdS7FOvV8My'\n",
    "API_secret = '8B16HiL0padJMA1cBvkYCU0lPJ8hqZ_l'\n",
    "img_01 = 'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Finews.gtimg.com%2Fnewsapp_bt%2F0%2F10985943880%2F641.jpg&refer=http%3A%2F%2Finews.gtimg.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1650870232&t=86ab568cb4ad2ab2a9917af637c67650'\n",
    "img_02 = 'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fqqpublic.qpic.cn%2Fqq_public%2F0%2F0-2587108314-6594DB0995FBBA388B26C2E35C730CDE%2F0%3Ffmt%3Djpg%26amp%3Bsize%3D45%26amp%3Bh%3D797%26amp%3Bw%3D512%26amp%3Bppv%3D1&refer=http%3A%2F%2Fqqpublic.qpic.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1650870232&t=fbf52493f18616b79f2052950e693b68'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "345b8bc5",
   "metadata": {},
   "outputs": [],
   "source": [
    "def detect_face(API_key,API_sercret,img_url):\n",
    "    \"\"\"face++ API人脸检测函数\"\"\"\n",
    "    base_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "    payload = {\n",
    "        'api_key':API_key,\n",
    "        'api_secret':API_sercret,\n",
    "        'image_url':img_url\n",
    "    }\n",
    "    r = requests.post(url = base_url, params = payload)\n",
    "    return r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "874f4d36",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648278784,54d84cb3-6622-495a-b054-6467af3abf49',\n",
       " 'time_used': 186,\n",
       " 'faces': [{'face_token': 'd69bb554deb5c109afa608133a494250',\n",
       "   'face_rectangle': {'top': 261, 'left': 223, 'width': 146, 'height': 146}}],\n",
       " 'image_id': 'SrjRxMmQoxZM1DBFftvmgQ==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "detect_face(API_key,API_secret,img_01)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "4d639747",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648278801,bbeec238-9e50-4fa2-8c0a-1f8bc577c9d8',\n",
       " 'time_used': 203,\n",
       " 'faces': [{'face_token': 'd7368906f3dc483594092fb0fb0c8947',\n",
       "   'face_rectangle': {'top': 273, 'left': 154, 'width': 134, 'height': 134}}],\n",
       " 'image_id': 'aDlB8SeyXgnGhk8CXX0kOw==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "detect_face(API_key,API_secret,img_02)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3fac7d9b",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
